<?php
/**
 *
 * Thrift Transport class
 * create and manage transport session through Thrift Api
 * @author haumv
 * @editedby nhudt
 * @version $Id: 01 ThriftTransportProfile.php 2012-3-14 11:31:16Z nhudt $
 *
 */
$GLOBALS['THRIFT_ROOT'] = ROOT_PATH . '/' . 'library/SGN/thrift';
// Load up all the thrift stuff
require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/transport/TBufferedTransport.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/transport/TFramedTransport.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/packages/sgn/sgnService.php';
include_once $GLOBALS['THRIFT_ROOT'] . '/packages/EsbService/EsbService.php';

class SGN_ThriftTransportProfile
{
    private $_socket = null;
    private $_transport = null;
    private $_protocol = null;
    private $_sgnServiceClient = null;
    private $_esbServiceClient = null;
    public function __construct ($options, $chooser = 'sgnService')
    {
//        $this->_socket = new TSocket($options['host'], $options['port'], TRUE);
//        $this->_transport = new TFramedTransport($this->_socket);
//        $this->_protocol = new TBinaryProtocolAccelerated($this->_transport);
//        $this->_sgnServiceClient = new sgnServiceClient($this->_protocol);
        
        $this->_socket = new TSocket($options['host'], $options['port'], TRUE);
        $this->_transport = new TFramedTransport($this->_socket);
        $this->_protocol = new TBinaryProtocolAccelerated($this->_transport);
        if($chooser === 'sgnService')$this->_sgnServiceClient = new sgnServiceClient($this->_protocol);
        else if($chooser === 'esbService') {
            $this->_esbServiceClient = new EsbServiceClient($this->_protocol);
        }
    }
    /* functions for sgnService */
    public function registerAccount ($accountId)
    {
        if($this->_sgnServiceClient === null)return;
        $this->openTransport();
        return $this->_sgnServiceClient->registerAccount($accountId);
    }
    public function getAccount ($accountId)
    {
        if($this->_sgnServiceClient === null)return;
        $this->openTransport();
        return $this->_sgnServiceClient->getAccount($accountId);
    }
    public function unRegisterAccount ($accountId)
    {
        if($this->_sgnServiceClient === null)return;
        $this->openTransport();
        return $this->_sgnServiceClient->unRegisterAccount($accountId);
    }
    public function createRelationship ($accountId1, $accountId2, $rtype)
    {
        if($this->_sgnServiceClient === null)return;
        $this->openTransport();
        return $this->_sgnServiceClient->createRelationship($accountId1, $accountId2, $rtype);
    }
    public function removeRelationship ($accountId1, $accountId2, $rtype)
    {
        if($this->_sgnServiceClient === null)return;
        $this->openTransport();
        return $this->_sgnServiceClient->removeRelationship($accountId1, $accountId2, $rtype);
    }
    public function pushFeed ($accountId, $message, $rtype)
    {
        if($this->_sgnServiceClient === null)return;
        $this->openTransport();
        return $this->_sgnServiceClient->pushFeed($accountId, $message, $rtype);
    }
    public function getMyFeed ($accountId, $pageNume, $pageSize)
    {
        if($this->_sgnServiceClient === null)return;
        $this->openTransport();
        return $this->_sgnServiceClient->getMyFeed($accountId, $pageNume, $pageSize);
    }
    public function getMyWallFeed ($accountId, $pageNume, $pageSize)
    {
        if($this->_sgnServiceClient === null)return;
        $this->openTransport();
        return $this->_sgnServiceClient->getMyWallFeed($accountId, $pageNume, $pageSize);
    }
    public function getFeeds($feedIds)
    {
        if($this->_sgnServiceClient === null)return;
        $this->openTransport();
        return $this->_sgnServiceClient->getFeeds($feedIds);
    }
	/* functions for esbService */
    public function requestFunction($functionName, $encodedInput, $authKey)
    {
        if($this->_esbServiceClient === null)return;
        $this->openTransport();
        return $this->_esbServiceClient->requestFunction($functionName, $encodedInput, $authKey);
    }
    
    /* common function */
    public function openTransport ()
    {
        if (! $this->_transport->isOpen())
            $this->_transport->open();
    }
    public function __destruct ()
    {
        if ($this->_transport->isOpen())
            $this->_transport->close();
    }
}